package com.bluezone.dec.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bluezone.dec.common.CacheConstant;
import com.bluezone.dec.common.JsonResult;
import com.bluezone.dec.system.service.RectPlanService;
import com.bluezone.dec.system.service.SpecialRectifStatisticsService;
import com.google.common.base.Strings;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HHL
 * @description 专项整治统计
 * @date 2018/10/22 - 13:05
 */
@Api(tags = "专项分析辅助决策/专项整治统计")
@RestController
@RequestMapping("/dec/spec-remediation")
public class SpecialRectifStatisticsController {

    @Autowired
    private RectPlanService rectPlanService;

    @Autowired
    private SpecialRectifStatisticsService rectifStatisticsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation("专项整治任务计划与任务实施情况")
    @GetMapping("/schedule-tasks-impl")
    public JsonResult getScheduleTasksImplement() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.SCHEDULE_TASKS_IMPLEMENT))) {
            Map<String, Object> scheduleTaskMapArray = rectifStatisticsService.getScheduleTasksImplement();
            return new JsonResult().ok(scheduleTaskMapArray);
        } else {
            String scheduleTasksImplementMap = redisTemplate.opsForValue().get(CacheConstant.SCHEDULE_TASKS_IMPLEMENT);
            /* json字符串转map */
            Map<String, Object> scheduleTaskMapArray = JSON.parseObject(scheduleTasksImplementMap, Map.class);
            return new JsonResult().ok(scheduleTaskMapArray);
        }
    }

    @ApiOperation("专项整治统计/[未完成整治企业统计]统计每一个专项整治计划对应的未完成整改的企业数")
    @GetMapping("/unfinish-counts")
    public JsonResult getUnfinishRectifNums() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.UNFINISH_RECTIFICATION_COUNT))) {
            List<Map<String, Object>> resultList = rectifStatisticsService.getUnfinishRectifNums();
            return new JsonResult().ok(resultList);
        } else {
            String unfinishRectifNumsList = redisTemplate.opsForValue().get(CacheConstant.UNFINISH_RECTIFICATION_COUNT);
            JSONArray result4AllPlanMapList = JSONArray.parseArray(unfinishRectifNumsList);
            return new JsonResult().ok(result4AllPlanMapList);
        }
    }

    @ApiOperation("获取  专项整治统计/专项整治任务普查情况")
    @GetMapping("/task-census")
    public JsonResult specialRectifTaskCensus() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.TASK_CENSUS))) {
            List<Map<String, Object>> resultList = rectifStatisticsService.getSpecialRectifTaskCensus();
            return new JsonResult().ok(resultList);
        } else {
            String planLevelCountList = redisTemplate.opsForValue().get(CacheConstant.TASK_CENSUS);
            JSONArray planLevelCountArray = JSONArray.parseArray(planLevelCountList);
            return new JsonResult().ok(planLevelCountArray);
        }
    }

    @ApiOperation("获取专项整理行动总数、整治普查情况、整治投入情况、整治进度情况等 头部统计信息")
    @GetMapping("/special-statis")
    public JsonResult getAllStatisticCounts() {

        Map map = new HashMap();

        /* 专项整理行动总数 */
        int actionCount = rectPlanService.getTotalPlanCounts();
        /* 整治普查情况 */
        int totalRecord = rectifStatisticsService.getTotalRecord();
        /* 整治投入情况 */
        int investCount = rectifStatisticsService.getTotalInvest();
        /* 整治进度情况 */
        int progressCount = rectifStatisticsService.getFinishedProgressCount();

        map.put("actionCount", actionCount);
        map.put("totalRecord", totalRecord);
        map.put("investCount", investCount);
        map.put("progressCount", progressCount);

        return new JsonResult().ok(map);
    }

    @ApiOperation("专项整治统计/专项整治企业投入情况")
    @GetMapping("/invest-statis")
    public JsonResult enterpriseRectificatInvest() {

        if (Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.INVEST_STATISTIC))) {
            List<Map<String, Object>> enterpriseRectificatInvest = rectifStatisticsService.getEnterpriseRectificatInvest();
            return new JsonResult().ok(enterpriseRectificatInvest);
        } else {
            String result4AllPlanList = redisTemplate.opsForValue().get(CacheConstant.INVEST_STATISTIC);
            JSONArray result4AllPlanListArray = JSONArray.parseArray(result4AllPlanList);
            return new JsonResult().ok(result4AllPlanListArray);
        }
    }

}
